package com.xinxing.learning.datasource.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinxing.learning.datasource.domain.convertor.OrderConvertor;
import com.xinxing.learning.datasource.domain.query.OrderInfoBaseQuery;
import com.xinxing.learning.datasource.domain.query.UserOrderInfoQuery;
import com.xinxing.learning.datasource.domain.request.UserOrderInfoRequest;
import com.xinxing.learning.datasource.domain.vo.OrderInfoVO;
import com.xinxing.learning.datasource.entity.OrderInfo;
import com.xinxing.learning.datasource.mapper.first.FirstUserOrderInfoMapper;
import com.xinxing.learning.datasource.mapper.second.SecondUserOrderInfoMapper;
import com.xinxing.learning.datasource.service.UserOrderInfoService;
import com.xinxing.learning.datasource.util.GenerateOrderNoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ruyuan
 * 用户订单服务实现
 */
@Slf4j
@Service
public class UserOrderInfoServiceImpl implements UserOrderInfoService {

    @Autowired
    private FirstUserOrderInfoMapper firstUserOrderInfoMapper;

    @Autowired
    private SecondUserOrderInfoMapper secondUserOrderInfoMapper;

    @Autowired
    private OrderConvertor orderConvertor;

    @Override
    public void generateOrder(UserOrderInfoRequest userOrderInfoRequest) {
        OrderInfo orderInfo = orderConvertor.dtoConvertOrderInfo(userOrderInfoRequest);
        orderInfo.setRemark(orderInfo.getRemark() + "master");
        String orderNo = GenerateOrderNoUtils.getOrderNo(orderInfo.getUserId(), orderInfo.getMerchantId());
        orderInfo.setOrderNo(orderNo);
        firstUserOrderInfoMapper.insertSelective(orderInfo);
    }

    /**
     * 根据用户id获得订单列表
     *
     * @param userOrderInfoQuery 入参
     * @return 出参
     */
    @Override
    public Page<OrderInfoVO> queryUserOrderInfoList(UserOrderInfoQuery userOrderInfoQuery) {
        OrderInfoBaseQuery orderInfoBaseQuery = orderConvertor.userQueryToBaseOrder(userOrderInfoQuery);
        //1.组装mybatis查询插件
        Page<OrderInfoVO> page = new Page<>();
        page.setCurrent(orderInfoBaseQuery.getPageNo());
        page.setSize(orderInfoBaseQuery.getPageSize());
        // 2.查询该用户订单信息
        return secondUserOrderInfoMapper.queryOrderInfoList(page, orderInfoBaseQuery);
    }
}